<template>
  <el-form label-width="150px" ref="formRef" :model="dataForm" :rules="dataRule">
    <el-form-item class="input" :label="i18n('course.intensify.point.title')" prop="title">
      <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('course.intensify.point.title')"/>
    </el-form-item>

    <el-form-item class="mavon" prop="content" :label="i18n('course.intensify.point.content')">
      <editor ref="editorRef" :content="dataForm.content"></editor>
    </el-form-item>

    <el-form-item :label="i18n('common.sort')" prop="sort">
      <el-input-number v-model="dataForm.sort" :min="0">
      </el-input-number>
    </el-form-item>

    <el-form-item :label="i18n('common.status')" prop="status">
      <el-switch
        v-model="dataForm.status"
        active-value="1"
        :active-text="i18n('common.enable')"
        inactive-value="2"
        :inactive-text="i18n('common.disable')">
      </el-switch>
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        @click="dataFormSubmit(formRef)">
        {{ i18n('common.confirm') }}
      </el-button>
      <el-button @click="resetForm(formRef)">
        {{ i18n('common.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup name="course-intensify-basic-index">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import Editor from '@/components/form/editor'

  const { router, query, i18n, isAuth } = useBase()

  const props = defineProps({
    model: {
      type: String,
      default: null,
    }
  });

  const editorRef = ref()

  const dataForm = reactive({
    id: '',
    course_id: 0,
    intensify_id: 0,
    type: 1,
    title: '',
    content: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('course.intensify.point.rules.title.require'),
      trigger: 'blur'
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(props.model, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${props.model}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.type = data.data.type.value
            dataForm.title = data.data.title
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${props.model}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'course_id': dataForm.course_id,
                'intensify_id': dataForm.intensify_id,
                'point_id': dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data)
                {
                  dataForm.content = data.data.content
                }
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${props.model}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id,
            course_id: dataForm.course_id,
            intensify_id: dataForm.intensify_id,
            type: dataForm.type,
            title: dataForm.title,
            content: editorRef.value.data,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(i18n(data.message))
          }
        })
      }
    })
  }

  onMounted(() => {
    dataForm.course_id = query.course_id || 0
    dataForm.intensify_id = query.intensify_id || 0

    init(formRef)
  })
</script>

<style lang="scss" scoped>
  .admin_form_main .el-form-item{
    .el-input {
      width: 400px;
    }
    .el-input-number {
      width: 400px;
    }
  }
</style>
